package com.jzg.jcptadmin.adpter;


import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.data.vo.OfflineDetectionData;
import com.jzg.template.refreshrecycleview.RefreshBaseAdapter;
import com.jzg.template.refreshrecycleview.RefreshEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jzg on 2015/12/31.
 */
public class OfflineDetectionAdapter extends RefreshBaseAdapter {


    private Context context;
    private List<OfflineDetectionData.OffLineTaskListEntity> lists;
    private AppContext appContext;


    public OfflineDetectionAdapter(Context context, List<OfflineDetectionData.OffLineTaskListEntity> lists) {
        super(context, lists);
        this.context = context;
        this.lists = lists;
        appContext = (AppContext) context.getApplicationContext();
    }

    @Override
    public RecyclerView.ViewHolder getHolderView(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.offline_detection_item, null));
    }

    @Override
    public int getItemCount() {
        if (lists.size() < 10)
            return lists.size();
        return super.getItemCount();
    }

    public void UpDateList(List<OfflineDetectionData.OffLineTaskListEntity> lists) {
        this.lists = lists;
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, int position, RefreshEntity entity) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        OfflineDetectionData.OffLineTaskListEntity entity1 = (OfflineDetectionData.OffLineTaskListEntity) entity;
        //myViewHolder.tvChepaihao.setText(entity1.getCarLicense());//车牌号
        if (entity1.getVin() != null) {
            myViewHolder.tvVin.setText("车架号: " + entity1.getVin());
        }

        if (entity1.getProvName().equals(entity1.getCityName())) {
            myViewHolder.area.setText(entity1.getProvName());
        } else {
            myViewHolder.area.setText(entity1.getProvName() + "-" + entity1.getCityName());
        }

        if (TextUtils.isEmpty(entity1.getBusinessPrice())) {
            myViewHolder.tvName.setText("暂无");//成交价
        } else {
            try {
                String str = entity1.getBusinessPrice();
                BigDecimal bd = new BigDecimal(Double.parseDouble(str));
                String string = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
                if (string.equals("0.0")) {
                    myViewHolder.tvName.setText("暂无");
                } else {
                    myViewHolder.tvName.setText(string + "万元");//成交价
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        // myViewHolder.tvName.setText(entity1.getLikeMan());//联系人
        // myViewHolder.tvPhone.setText(entity1.getLikeTel());//联系电话
        myViewHolder.itemBuyCarSeeTimes.setText(entity1.getCreateTime());
        ImageLoader.getInstance().displayImage(entity1.getImagePath(), myViewHolder.itemBuyCarIcon, appContext.options);
        ImageLoader.getInstance().displayImage(entity1.getStatusImg(), myViewHolder.itemBuyCarPrice, appContext.options);
        //产品类型
        if (!TextUtils.isEmpty(entity1.getProductTypeImage())) {
            myViewHolder.item_product_style.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().
                    displayImage(entity1.getProductTypeImage(), myViewHolder
                            .item_product_style, appContext.options);
        } else {
            myViewHolder.item_product_style.setVisibility(View.GONE);
        }

        if (entity1.getIsShowPrice().equals("0")) {
            myViewHolder.llPrice.setVisibility(View.VISIBLE);
            myViewHolder.tvPrice.setText("暂无估值");
        } else if (entity1.getIsShowPrice().equals("1")) {
            myViewHolder.llPrice.setVisibility(View.VISIBLE);
//            String[] price = entity1.getAppraisePrice().split("\\.");
//            if (price.length > 1) {
//                if (price[1].length() > 2) {
//                    String price1 = entity1.getAppraisePrice().substring(0, entity1.getAppraisePrice().length() - 2);
//                    myViewHolder.tvPrice.setText(price1 + "万元");
//                } else {
//                    myViewHolder.tvPrice.setText(entity1.getAppraisePrice() + " 万元");
//                }
//            } else {
//                myViewHolder.tvPrice.setText(entity1.getAppraisePrice() + " 万元");
//            }
            try {
                String str = entity1.getAppraisePrice();
                BigDecimal bd = new BigDecimal(Double.parseDouble(str));
                DecimalFormat fnum = new DecimalFormat("##0.00");
                myViewHolder.tvPrice.setText("估值：" + fnum.format(bd) + "万元");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_buy_car_icon)
        ImageView itemBuyCarIcon;
        @Bind(R.id.tv_chepaihao)
        TextView tvChepaihao;
        @Bind(R.id.tv_vin)
        TextView tvVin;
        @Bind(R.id.area)
        TextView area;
        @Bind(R.id.item_buy_car_rightcenter)
        LinearLayout itemBuyCarRightcenter;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.item_buy_car_price)
        ImageView itemBuyCarPrice;
        @Bind(R.id.item_buy_car_rightcenter2)
        LinearLayout itemBuyCarRightcenter2;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.ll_price)
        LinearLayout llPrice;
        @Bind(R.id.item_buy_car_see_times)
        TextView itemBuyCarSeeTimes;
        @Bind(R.id.relativeLayout)
        RelativeLayout relativeLayout;
        @Bind(R.id.item_product_style)
        ImageView item_product_style;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
